package com.tommy.ddp.manager
{
	import com.tommy.ddp.GridDataManager;
	import com.tommy.ddp.MainManager;
	import com.tommy.ddp.box.BoxFactory;
	import com.tommy.ddp.box.DdpBox;
	import com.tommy.ddp.definition.AxisConfig;

	public class Finder
	{
		private var _box:DdpBox;
		
		private var boxCollection:Array;
		
		private var finalArr:Array;
		
		public function Finder()
		{
			boxCollection = [new Array(),new Array(),new Array(),new Array()];
			finalArr = [];
		}
		
		public function start(box:DdpBox):void
		{
			_box = box;
			//var t=getTimer();
			
			find(_box,AxisConfig.X_AXIS,1,boxCollection[0]);
			find(_box,AxisConfig.X_AXIS,-1,boxCollection[1]);
			find(_box,AxisConfig.Y_AXIS,1,boxCollection[2]);
			find(_box,AxisConfig.Y_AXIS,-1,boxCollection[3]);
			
			
			var xArr:Array = [];
			var yArr:Array = [];
			
			xArr = xArr.concat(boxCollection[0]);
			for each(var i:DdpBox in boxCollection[1])
			{
				if(xArr.indexOf(i) == -1)
					xArr.push(i);
			}
			
			yArr = yArr.concat(boxCollection[2]);
			for each(i in boxCollection[3])
			{
				if(yArr.indexOf(i) == -1)
					yArr.push(i);
			}
			//
			if(xArr.length >= 3)
				finalArr.push(xArr);
			else
				finalArr.push([]);
			//
			if(yArr.length >= 3)
				finalArr.push(yArr);
			else
				finalArr.push([]);
			
			//trace(getTimer()-t);
		}
		
		private function find(box:DdpBox,axis:int,dir:int,arr:Array):void
		{
			arr.push(box);
			
			var nextBox:DdpBox;
			if(axis == AxisConfig.X_AXIS)
				nextBox = GridDataManager.getBox(box.column+dir,box.row);
			else
				nextBox = GridDataManager.getBox(box.column,box.row+dir);
			
			if(!nextBox)
				return;
			else if(box.colorType != nextBox.colorType)
				return;
			
			find(nextBox,axis,dir,arr);
		}
		
		/**
		 * 获得被消除的BOX的“列”的索引集合
		 * @return 
		 * 
		 */		
		public function get columnList():Vector.<int>
		{
			var vect:Vector.<int> = new Vector.<int>();
			for each(var i:Array in finalArr)
			{
				for each(var j:DdpBox in i)
				{
					vect.push(j.column);
				}
			}
			return vect;
		}
		
		/**
		 * 是否有交叉
		 * @return 
		 * 
		 */		
		public function get isCross():Boolean
		{
			return finalArr[0].length > 3 && finalArr[1].length > 3 && maxLength < 5;
		}
		
		/**
		 * 横向或纵向最大的长度
		 * @return 
		 * 
		 */		
		public function get maxLength():uint
		{
			return Math.max(finalArr[0].length,finalArr[1].length);
		}
		/**
		 * 消除棋子，同时检查有没有需要生成新的
		 * 
		 */		
		public function dispose():void
		{
			for each(var i:Array in finalArr)
			{
				for each(var j:DdpBox in i)
				{
					j.dispose();
				}
			}
			//
			createNew();
			
			_box = null;
			boxCollection = null;
			finalArr = null;
		}
		
		private function createNew():void
		{
			if(maxLength > 3)
			{
				var box:DdpBox = BoxFactory.createRandomBox(_box.column,_box.row);
				MainManager.game.container.addChild(box);
			}
		}
		
		/*public function ss():void
		{
			for each(var i:Array in finalArr)
			{
				for each(var j:DdpBox in i)
				{
					j.ss();
				}
			}
		}*/
	}
}